<template>
  <div class="login-container">
    <el-form ref="form" :model="form" label-width="80px" class="login-form">
      <h2 class="login-title">管理系统</h2>
      <el-form-item label="用户名">
        <el-input v-model="form.username" @keyup.native.enter="onSubmit"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password" type="password" @keyup.native.enter="onSubmit"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

import {
  login,
} from "./login";
import axios from 'axios'
export default {
  data() {
    return {
      form: {
        username: "",
        password: ""
      }
    };
  },
  methods: {
    onSubmit() {
      console.log("submit!");


      login(this.form.username, this.form.password).then((res) => {
        if (res.code == 200) {

          var user = res.data;
          console.log(user, '登录用户信息');

          sessionStorage.setItem("soa_id", user.id)
          sessionStorage.setItem("soa_account", user.account)
          sessionStorage.setItem("soa_token", user.token)

          switch (user.unitType) {
            case 'system': //系统消息 
              sessionStorage.setItem("logOnUser", JSON.stringify(user))
              this.$router.push('/initializationHome')
              break
            case 'agency': //机关消息 
            if(!user.postInfo.unitId){
              this.$message.error('该账号缺少单位，请联系管理员')
              return
            }
              switch (user.postInfo.serviceStatus) {
                case 0: //未开通服务
                  alert('您的服务未开通，请联系管理员')
                  return
                case 1: //已开通服务
                  break
                case 2: //已到期服务
                  alert('您的服务已到期，请联系管理员')
                  return
                case 3: //已中断服务
                  alert('您的服务已中断，请联系管理员')
                  return
              }
              let params = {
                unitName: user.postInfo.unitName,
                unitId: user.postInfo.unitId,
                unitType: user.unitType,
                isSuperAdmin: user.postInfo.isSuperAdmin,
                id: user.id,
                name: user.name,
                departmentId: user.departmentId,
                departmentName: user.departmentName,
                account: user.account,
                token: user.token,
                email: user.email,
                mobile: user.mobile,
                roleIds: user.roleIds,
              }
              sessionStorage.setItem("logOnUser", JSON.stringify(params))
              let userData = JSON.parse(sessionStorage.getItem('logOnUser'))
              console.log(user, "=====================")
              console.log(userData, "=====================")
              this.$router.push('/initializationHome')
              break
            case 'company': //机构消息
              if (user.postInfo.postInfoList.length == 0) {
                this.$message.error('该账号缺少单位，请联系管理员')
                return
              }
              if (user.postInfo.postInfoList.length > 1) {
                this.$router.push('/SelectCompany')
                sessionStorage.setItem("SelectCompanyUserData", JSON.stringify(user))
              } else {
                switch (user.postInfo.postInfoList[0].serviceStatus) {
                  case 0: //未开通服务
                    alert('您的服务未开通，请联系管理员')
                    return
                  case 1: //已开通服务
                    break
                  case 2: //已到期服务
                    alert('您的服务已到期，请联系管理员')
                    return
                  case 3: //已中断服务
                    alert('您的服务已中断，请联系管理员')
                    return
                }
                let params = {
                  unitName: user.postInfo.postInfoList[0].unitName,
                  unitId: user.postInfo.postInfoList[0].unitId,
                  unitType: user.postInfo.postInfoList[0].unitType,
                  isSuperAdmin: user.postInfo.postInfoList[0].isSuperAdmin,
                  id: user.id,
                  name: user.name,
                  departmentId: user.departmentId,
                  departmentName: user.departmentName,
                  account: user.account,
                  token: user.token,
                  email: user.email,
                  mobile: user.mobile,
                  roleIds: user.roleIds,
                }
                sessionStorage.setItem("logOnUser", JSON.stringify(params))
                this.$router.push('/initializationHome')
              }
              break
          }


          this.$message({
            message: "登录成功",
            type: "success",
          });

        } else {
          this.$message({
            message: res.message,
            type: "warning",
          });
        }
      });
    }
  }
};
</script>

<style scoped>
.login-form {
  width: 350px;
  margin: 160px auto;
  /* 上下间距160px，左右自动居中*/
  background-color: rgb(255, 255, 255, 0.8);
  /* 透明背景色 */
  padding: 30px;
  border-radius: 20px;
  /* 圆角 */
}

/* 背景 */
.login-container {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: url("../../../assets/login.jpg") no-repeat 0px 0px;
  background-size: cover;
}

/* 标题 */
.login-title {
  color: #303133;
  text-align: center;
}
</style>